﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ZombieMonkeys.Vibria.Logic;
using ZombieMonkeys.Vibria.DataLayer;

namespace ZombieMonkeys.Vibria
{
    public partial class ButtonGrid : UserControl
    {
        //reference the app page of the application for access to global variables
        GUI.App app;
    
        /// <summary>
        /// Instantiates the ButtonGrid control
        /// </summary>
        public ButtonGrid()
        {
            InitializeComponent();

            if (Application.Current != null)
            {
                app = ((GUI.App)Application.Current);
            }

            if (app.HintMode == false)
            {
                hintButton.Visibility = Visibility.Collapsed;
            }
            if (app.PenMode == true)
            {
                button10.IsEnabled = false;
            }
        }

        //holds the Gridview object for reference by this class
        ZombieMonkeys.Vibria.GUI.GridView gridView;
        public GUI.GridView SetGridView
        {
            get
            {
                return gridView;
            }
            set
            {
                gridView = value;
            }
        }

        /// <summary>
        /// Gets the tag of the button that was clicked in the grid and sends it to the square in the puzzlegrid
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonGrid_Click(object sender, RoutedEventArgs e)
        {
            string buttonValue = ((Button)sender).Tag.ToString();
            gridView.puzzleGrid1.updateSquare(buttonValue);
            gridView.CheckPuzzle();
        }

        /// <summary>
        /// Method to handle the hint button click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HintButton_Click(object sender, RoutedEventArgs e)
        {
            gridView.GiveMeAHint();
        }

        /// <summary>
        /// Method to handle the solve button click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SolveButton_Click(object sender, RoutedEventArgs e)
        {
            if ((string)solveButton.Content == "Solve")
            {
                gridView.SolveMyPuzzle();
            }
            else
            {
                Puzzle puzzle = new Puzzle(new ZombieMonkeys.Vibria.Logic.Grid(gridView.puzzleGrid1.puzzleGridList));
                PuzzleSaver saver = new PuzzleSaver();
                saver.Save(puzzle);
                Uri mainMenu = new Uri(string.Format("/MainPage.xaml"), UriKind.Relative);
                this.gridView.NavigationService.Navigate(mainMenu);
            }
        }

        /// <summary>
        /// method for vertical rotation code
        /// </summary>
        public void RotateVertical()
        {
            upperPanel.Orientation = Orientation.Vertical;
            lowerPanel.Orientation = Orientation.Vertical;
            outerPanel.Orientation = Orientation.Horizontal;
        }

        /// <summary>
        /// Method for horizintal rotation code
        /// </summary>
        public void RotateHorizontal()
        {
            upperPanel.Orientation = Orientation.Horizontal;
            lowerPanel.Orientation = Orientation.Horizontal;
            outerPanel.Orientation = Orientation.Vertical;
            //time block is only visible when the title panel is invisible
            timeBlock.Visibility = Visibility.Collapsed;
        }
    }
}
